/*******************************************************************************
Name: create_figure3
*******************************************************************************/


clear

capture log close

log using  "${logdir}8-create_figure3.log", replace


/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
*COLLAPSE IPUMS CENSUS DATA INTO (AGGREGATED) CELLS (BY COHORT AND RACE)
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/



foreach year of numlist 1970 2000 {
	
use "${rawdir}ipums_`year'.dta", clear

drop if metarea == 0
keep if age >= 16 & age <= 64
keep if (race == 1 | race == 2) & hispan == 0

keep if gq == 1

gen cohort_cat = 0
replace cohort_cat = 4 if age >= 25 & age <= 34 & year == 1970
replace cohort_cat = 3 if age >= 35 & age <= 44 & year == 1970
replace cohort_cat = 2 if age >= 45 & age <= 54 & year == 1970
replace cohort_cat = 1 if age >= 55 & age <= 64 & year == 1970

replace cohort_cat = 7 if age >= 25 & age <= 34 & year == 2000
replace cohort_cat = 6 if age >= 35 & age <= 44 & year == 2000
replace cohort_cat = 5 if age >= 45 & age <= 54 & year == 2000
replace cohort_cat = 4 if age >= 55 & age <= 64 & year == 2000

keep if cohort_cat > 0

gen age_cat = 0
replace age_cat = 1 if age >= 25 & age <= 34
replace age_cat = 2 if age >= 35 & age <= 44
replace age_cat = 3 if age >= 45 & age <= 54
replace age_cat = 4 if age >= 55 & age <= 64


*census regions
rename region division
gen region = floor(division/10)

replace division = 21 if metarea == 164
replace region = 2 if metarea == 164

replace division = 31 if metarea == 340
replace region = 3 if metarea == 340

replace division = 32 if metarea == 452
replace region = 3 if metarea == 452

replace division = 31 if metarea == 916
replace region = 3 if metarea == 916

drop if region == 9

gen employed = (empstat == 1)

if `year' == 1970 {
	gen inschool = gradeatt > 0 & gradeatt != .
} 
else {
	gen inschool = (school == 2)
}

gen idle = !employed & !inschool

egen count = count(year), by(year metarea age_cat cohort_cat race)

collapse (mean) employed idle incwage count [aweight = perwt], by(year metarea age_cat cohort_cat race)


save "${datadir}figure_ls_`year'.dta", replace

}

/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
*FIGURE 3: JOB SUBURBANIZTION AND CHANGES IN EMPLOYMENT RATES, 1970-2000
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/



use "${datadir}figure_ls_1970.dta", clear
append using "${datadir}figure_ls_2000.dta"



merge metarea year using "${datadir}pop_working.dta", sort uniqusing

tab _merge
keep if _merge == 3
drop _merge

merge metarea using "${datadir}matched_msas.dta", sort uniqusing

keep if _merge == 3
drop _merge

gen group = 10*cohort_cat + race

gen groupXmetarea = 100*metarea + group

gen order = 0
replace order = 1 if year == 1970
replace order = 2 if year == 2000


gen size50 = count >= 50
gen size25 = count >= 25

tsset groupXmetarea order

*suburbanization outcomes

gen fcc = emp_tot_cc/emp_tot

gen ln_fcc = ln(fcc)
gen ln_fcc_fd = ln_fcc - L1.ln_fcc

gen ln_tot = ln(emp_tot)
gen ln_tot_fd = ln_tot - L1.ln_tot


gen active = 1 - idle
gen ln_active = ln(1 - idle)

gen active_fd = active - L1.active

gen ln_active_fd = ln_active - L1.ln_active

gen ln_inc = ln(incwage)

gen ln_inc_fd = ln_inc - L1.ln_inc

gen black = (race == 2)



*new MSA labels
gen metarea2 = metarea	
	
label define metarea2_lbl 008 `"Akron"'
label define metarea2_lbl 052 `"Atlanta"', add
label define metarea2_lbl 060 `"Augusta"', add
label define metarea2_lbl 072 `"Baltimore"', add
label define metarea2_lbl 076 `"Baton Rouge"', add
label define metarea2_lbl 084 `"Beaumont"', add
label define metarea2_lbl 100 `"Birmingham"', add
label define metarea2_lbl 112 `"Boston"', add
label define metarea2_lbl 116 `"Bridgeport"', add
label define metarea2_lbl 128 `"Buffalo"', add
label define metarea2_lbl 144 `"Charleston"', add
label define metarea2_lbl 152 `"Charlotte"', add
label define metarea2_lbl 156 `"Chattanooga"', add
label define metarea2_lbl 160 `"Chicago"', add
label define metarea2_lbl 164 `"Cincinnati"', add
label define metarea2_lbl 168 `"Cleveland"', add
label define metarea2_lbl 176 `"Columbia"', add
label define metarea2_lbl 184 `"Columbus"', add
label define metarea2_lbl 192 `"Dallas"', add
label define metarea2_lbl 200 `"Dayton"', add
label define metarea2_lbl 208 `"Denver"', add
label define metarea2_lbl 216 `"Detroit"', add
label define metarea2_lbl 264 `"Flint"', add
label define metarea2_lbl 268 `"Fort Lauderdale"', add
label define metarea2_lbl 312 `"Greensboro"', add
label define metarea2_lbl 316 `"Greenville"', add
label define metarea2_lbl 328 `"Hartford"', add
label define metarea2_lbl 336 `"Houston"', add
label define metarea2_lbl 348 `"Indianapolis"', add
label define metarea2_lbl 356 `"Jackson"', add
label define metarea2_lbl 359 `"Jacksonville"', add
label define metarea2_lbl 376 `"Kansas City"', add
label define metarea2_lbl 384 `"Knoxville"', add
label define metarea2_lbl 440 `"Little Rock"', add
label define metarea2_lbl 448 `"Los Angeles"', add
label define metarea2_lbl 452 `"Louisville"', add
label define metarea2_lbl 492 `"Memphis"', add
label define metarea2_lbl 500 `"Miami"', add
label define metarea2_lbl 508 `"Milwaukee"', add
label define metarea2_lbl 512 `"Twin Cities"', add
label define metarea2_lbl 516 `"Mobile"', add
label define metarea2_lbl 536 `"Nashville"', add
label define metarea2_lbl 548 `"New Haven"', add
label define metarea2_lbl 556 `"New Orleans"', add
label define metarea2_lbl 560 `"New York"', add
label define metarea2_lbl 572 `"Norfolk"', add
label define metarea2_lbl 588 `"Oklahoma City"', add
label define metarea2_lbl 592 `"Omaha"', add
label define metarea2_lbl 596 `"Orlando"', add
label define metarea2_lbl 616 `"Philadelphia"', add
label define metarea2_lbl 620 `"Phoenix"', add
label define metarea2_lbl 628 `"Pittsburgh"', add
label define metarea2_lbl 676 `"Richmond"', add
label define metarea2_lbl 678 `"Riverside"', add
label define metarea2_lbl 684 `"Rochester"', add
label define metarea2_lbl 692 `"Sacramento"', add
label define metarea2_lbl 704 `"St. Louis"', add
label define metarea2_lbl 724 `"San Antonio"', add
label define metarea2_lbl 732 `"San Diego"', add
label define metarea2_lbl 736 `"San Francisco"', add
label define metarea2_lbl 760 `"Seattle"', add
label define metarea2_lbl 768 `"Shreveport"', add
label define metarea2_lbl 816 `"Syracuse"', add
label define metarea2_lbl 828 `"Tampa"', add
label define metarea2_lbl 840 `"Toledo"', add
label define metarea2_lbl 848 `"Trenton"', add
label define metarea2_lbl 856 `"Tulsa"', add
label define metarea2_lbl 884 `"DC"', add
label define metarea2_lbl 896 `"West Palm Beach"', add
label define metarea2_lbl 916 `"Wilmington"', add
label define metarea2_lbl 932 `"Youngstown"', add
label values metarea2 metarea2_lbl



reg ln_active_fd  ln_fcc_fd [aw = count] if year == 2000 & black
local slope_b "Slope = `:di %6.3f _b[ln_fcc_fd]'"
local slope_b_se "             (`: di %4.3f _se[ln_fcc_fd]')"

graph twoway (scatter ln_active_fd  ln_fcc_fd [aw = count] if year == 2000 & black, mcolor(gs4) msymbol(circle_hollow)) ///
	(lfit ln_active_fd  ln_fcc_fd [aw = count] if year == 2000 & metarea != 60 & black, lcolor(gs10)), ///
	ytitle("Change in Log(Black Employment Rate)") xtitle("Change in Log(Fraction of MSA Jobs in Central City)") ///
	legend(off) scheme(s2color) plotregion(fcolor(white)) graphregion(fcolor(white)) ///
	text(-0.05  -0.75 "`slope_b'", place(e)  size(medium)) ///
	text(-0.075 -0.75 "`slope_b_se'", place(e)  size(medium))
	
graph save "${outdir}panel_black.gph", replace
graph export "${outdir}panel_black.pdf", replace


reg ln_active_fd  ln_fcc_fd [aw = count] if year == 2000 & !black
local slope_w "Slope = `:di %6.3f _b[ln_fcc_fd]'"
local slope_w_se "             (`: di %4.3f _se[ln_fcc_fd]')"

graph twoway (scatter ln_active_fd  ln_fcc_fd [aw = count] if year == 2000 & !black, mcolor(gs4) msymbol(circle_hollow)) ///
	(lfit ln_active_fd  ln_fcc_fd [aw = count] if year == 2000 & metarea != 60 & !black, lcolor(gs10)), ///
	ytitle("Change in Log(White Employment Rate)") xtitle("Change in Log(Fraction of MSA Jobs in Central City)") ///
	legend(off) scheme(s2color) plotregion(fcolor(white)) graphregion(fcolor(white)) ///
	text(-0.25  -0.75 "`slope_w'", place(e)  size(medium)) ///
	text(-0.265 -0.75 "`slope_w_se'", place(e)  size(medium))
	


graph save "${outdir}panel_white.gph", replace	
graph export "${outdir}panel_white.pdf", replace



log close


